//
//  ServerMultipier.swift
//  API
//
//  Created by ET|冰琳 on 2017/3/24.
//  Copyright © 2017年 IB. All rights reserved.
//

import Foundation
import ReactiveSwift
import Alamofire


/// 鉴于APP可能使用多个服务器
/// 每一个服务器返回的数据结构可能不一致
/// 故可对每一个服务器建立一个单独的request 并对结果进行解析，以保证最终在开发过程中需要的内容仅为需要的内容
///
/// 对应server的各种path可置于对应server的文件中
///
/// eg: {"msg": "suceed", "code": 0, "data": ... }       //假设调用处仅关心data
/// or eg: {"message" : "", "error": 1, "result": ... }  //假设调用处仅关心result
/// 则可在对应的server中处理下数据 ，仅传递上述关心的字段数据


/*
// MARK: - temple
extension Server {
    struct Multipier { }
}


// MARK: - server
extension Server.Multipier {
 
    fileprivate static let product = Server(scheme: "http", host: "api.github.com", basePath: "")
    fileprivate static let develop = Server(scheme: "http", host: "api.github.com", basePath: "")
    
    static let v1_0_0 = Server(scheme: "http", host: "api.github.com", basePath: "/v1")
    static let v2_0_0 = Server(scheme: "http", host: "api.github.com", basePath: "/v2")
}

extension Server.Multipier {
    
    static var host: Server {
        #if DEBUG
            return self.develop
        #else
            return self.product
        #endif
    }
    
    static func request(
        server: Server = Server.Multipier.host,
        path: Path,
        method: Alamofire.HTTPMethod = .get,
        parameters: Parameters? = nil,
        encoding: ParameterEncoding = URLEncoding.default,
        headers: HTTPHeaders? = nil)
        -> SignalProducer<Any, NSError>
    {
        let urlO = server.url(path)   
        guard let url = urlO else {
            fatalError("please provide a right url")
        }
        return API.request(url, method: method, parameters: parameters, encoding: encoding, headers: headers).reactive.responseJSON()
    }
}


extension Server.Multipier {
    
    func test() {
        _ = Server.Multipier.request(path: .login)
        _ = Server.Multipier.request(server: Server.Multipier.v1_0_0 ,path: .login, method: .get, parameters: nil)
        _ = Server.Multipier.request(server: Server.Multipier.v2_0_0 ,path: .login)
    }
    
}
*/
